package com.adcc.zkdemo.core;

import java.util.List;
import java.util.UUID;

import javassist.NotFoundException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("airportService")
public class RepositoryAirportService implements AirportService {

	@Autowired
	private AirportRepository repository;

	// --------------------------------------------------------------------------

	
	//UnitTest:core-airport-2
	@Transactional(rollbackFor = NotFoundException.class)
	public Airport add(Airport airport) {
		Airport newOne = new Airport();
		newOne.setCode3(airport.getCode3());
		newOne.setCode4(airport.getCode4());
        newOne.setAbbrZh(airport.getAbbrZh());
		// UUID
		newOne.setUuid(UUID.randomUUID().toString());

		return repository.save(newOne);
	}

    public void Del(String id)
    {
        repository.delete(Long.valueOf(id));
    }

    public void Updata(String id,String code3,String code4)
    {
        Airport oldEntity = repository.findOne(Long.valueOf(id));
        if(oldEntity != null)
        {
            oldEntity.setCode3(code3);
            oldEntity.setCode4(code4);
            repository.save(oldEntity);
        }
    }

    public List<Airport> getAll()
    {
        return repository.findAll();
    }

    public Airport findById(String id)
    {
        return repository.findOne(Long.valueOf(id));
    }

    public int getCount()
    {
        return (int)repository.count();
    }
	
}
